package com.canyou.archives.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.archives.model.Category;
import com.canyou.archives.mapper.CategoryMapper;
import com.canyou.archives.model.dto.CategoryDTO;
import com.canyou.archives.model.dto.CategoryExportExcelDTO;
import com.canyou.archives.model.dto.CategoryInportExcelDTO;
import com.canyou.archives.model.dto.CategoryPageDTO;
import com.canyou.archives.model.vo.PageVO;
import com.canyou.archives.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.canyou.utils.ExcelUtils;
import com.canyou.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备分类 服务实现类
 * </p>
 *
 * @author canyou
 * @since 2021-12-07
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    //新增编辑设备分类
    @Override
    public Category saveOrUpdateCategory(@Valid @NotNull(message = "设备分类信息不能为空") CategoryDTO categoryDTO) {
        Category category = new Category();
        if(StringUtils.isNull(categoryDTO.getId())){  //新增
            //分类名称不能为空
            if(StringUtils.isEmpty(categoryDTO.getCname())){
                return null;
            }
            //组装插入数据
            BeanUtils.copyProperties(categoryDTO, category);
        }else {  //修改
            //根据id查询判断,没有数据就直接返回
            Category byId = this.getById(categoryDTO.getId());
            if(StringUtils.isNull(byId)){
                return null;
            }
            //封装修改数据
            if(!StringUtils.isEmpty(categoryDTO.getCname())){
                byId.setCname(categoryDTO.getCname());  //名称
            }
            if(!StringUtils.isNull(categoryDTO.getSort())){
                byId.setSort(categoryDTO.getSort());  //排序
            }
            if(!StringUtils.isNull(categoryDTO.getState())){
                byId.setState(categoryDTO.getState());  //状态
            }
            BeanUtils.copyProperties(byId, category);
        }
        //判断操作成功
        boolean b = this.saveOrUpdate(category);
        if(!b){
             return null;
        }
        return category;
    }

    //条件分页查询分页
    @Override
    public PageVO getPageList(int current, int pageSize, CategoryPageDTO categoryPageDTO) {
        Page<Category> page = new Page<>(current,pageSize);
        //封装条件
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if(StringUtils.isNotNull(categoryPageDTO)){
            if(StringUtils.isNotEmpty(categoryPageDTO.getCname())){
                //模糊查询:名称
                wrapper.like("cname",categoryPageDTO.getCname());
            }
            if(StringUtils.isNotNull(categoryPageDTO.getSort())){
                //排序
                wrapper.orderByAsc("sort");
            }
            if(StringUtils.isNotNull(categoryPageDTO.getState())){
                //精确查询:状态
                wrapper.eq("state",categoryPageDTO.getState());
            }
        }
        IPage<Category> iPage = this.page(page, wrapper);
        //组装data
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    //查询所有数据
    @Override
    public PageVO getCategoryAll() {
        //封装条件
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Category> equCategoryList = this.list(wrapper);
        //判断数据有效性
        if(StringUtils.isNotEmpty(equCategoryList)){
            PageVO categoryPageVO = new PageVO();
            categoryPageVO.setRows(equCategoryList);
            return categoryPageVO;
        }
        return null;
    }

    //设备分类Excel导出
    @Override
    public Boolean exportExcel(HttpServletResponse response) {
        //查询数据
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Category> categoryList = this.list(wrapper);
        //得到空数据就直接返回
        if(StringUtils.isEmpty(categoryList)){
            return false;
        }
        //封装数据
        List<CategoryExportExcelDTO>list= new ArrayList<CategoryExportExcelDTO>();
        for(Category category:categoryList){
            CategoryExportExcelDTO categoryExcelDTO = new CategoryExportExcelDTO();
            BeanUtils.copyProperties(category, categoryExcelDTO);
            list.add(categoryExcelDTO);
        }
        ExcelUtils.exportExcel(list, "设备分类", "分类列表", CategoryExportExcelDTO.class, "设备分类列表.xlsx", response);
        return true;
    }
    //设备分类Excel导入
    @Override
    public Boolean importExcel(@NotNull(message = "用户excel文件不能为空") MultipartFile file) throws Exception{
        //读取Excel数据
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<CategoryInportExcelDTO>list= ExcelImportUtil.importExcel(file.getInputStream(), CategoryInportExcelDTO.class, params);
        //如果读取的集合为空
        if(StringUtils.isEmpty(list)){
            return false;
        }
        //将数据存入数据库
        for (CategoryInportExcelDTO dto:list){
            Category category = new Category();
            BeanUtils.copyProperties(dto, category);
            this.save(category);
        }
        return true;
    }


}
